<?php

namespace app\model\api;

use laytp\BaseModel;

class Mini extends BaseModel
{
    protected $name = 'mini_record';

    /**
     * 列表
     * @param $keyword
     * @param $type
     * @param $second_type
     * @param $limit
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function list($keyword, $type, $second_type, $limit = 30)
    {
        $where = [];
        if ($keyword) {
            $where[] = ["name", 'like', '%' . $keyword . '%'];
        }
        if ($type) {
            $where[] = ["primary_type", '=', $type];
        }
        if ($second_type) {
            $where[] = ["second_type", '=', $second_type];
        }
        $list = $this->where($where)
            ->where('if_show',1)
            ->cache(true)
            ->paginate($limit)
            ->toArray();
        //图片
        foreach ($list['data'] as &$item) {
            switch ($item['primary_type']) {
                case '工具':
                    $item['jump'] = 1;
                    if ($item['second_type'] == '生物') {
                        $item['jump'] = 2;
                    }
                    break;
                case '坐骑':
                    $item['jump'] = 3;
                    break;
                case '装扮':
                    $item['jump'] = 4;
                    break;
                default:
                    $item['jump'] = 1;
            }
        }
        return $list;
    }

    /**
     * 详情
     * @param $record_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($record_id)
    {
        $recordInfo = $this->where('id', $record_id)->cache(true)->find();
        $type = $recordInfo['primary_type'];
        $detail = [
            "primary_type" => $recordInfo['primary_type'],
            "second_type" => $recordInfo['second_type'],
            "name" => $recordInfo['name'],
            "img" => $recordInfo['img_url'],
        ];

        if(in_array($type,['作物','工具','杂物'])){
            switch ($type){
                case '作物':
                    $detail = array_merge($detail, (new MiniCrop())->getCropInfo($record_id)->toArray());
                    break;
                case '工具':
                    $detail = array_merge($detail, (new MiniTool())->getToolInfo($record_id)->toArray());
                    if ($detail['second_type'] == '生物') {
                        $detail['feed']       = json_decode($detail['feed_img'],true);
                        $detail['drop_items'] = json_decode($detail['drop_items_img'],true);
                    }
                    break;
                case '杂物':
                    $detail = array_merge($detail, (new MiniVaria())->getVariaInfo($record_id)->toArray());
                    break;
            }
            $detail['formula']      = json_decode($detail['formula'],true);
            $detail['formula_tool'] = json_decode($detail['formula_tool'],true);
            $detail['match_good']   = json_decode($detail['match_good'],true);
        }else{
            switch ($type){
                case '坐骑':
                    unset($detail['img']);
                    $detail = array_merge($detail, (new MiniMount())->getMountInfo($record_id)->toArray());
                    $detail['upgrade'] = (new MiniMount())->upgradeMount($detail['id']);
                    $detail['skill']   = json_decode($detail['skill'],true);
                    break;
                case '装扮':
                    $detail = array_merge($detail, (new MiniAttire())->getAttireInfo($record_id)->toArray());
                    $detail['attire_effect'] = json_decode($detail['attire_effect'],true);
                    $detail['get_way']       = json_decode($detail['get_way'],true);
                    break;
            }
        }

        return $detail;
    }

    public function typeList()
    {
        $typeList = (new MiniType())->getFirstType()->toArray();
        foreach ($typeList as &$item) {
            //获取二次分类
            $secondList = (new MiniType())->getSecondType($item['id'])->toArray();
            $item['second_list'] = $secondList;
        }
        return $typeList;
    }

}